package com.rc.evcharger.service.member;

import com.rc.evcharger.mapper.member.MemberAccountRecordMapper;
import com.rc.evcharger.model.member.MemberAccountRecord;
import com.rc.evcharger.model.member.MemberAccountRecordExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by sven on 2019/09/23.
 */
@Service
public class MemberAccountRecordServiceImpl implements MemberAccountRecordService {

    @Autowired
    private MemberAccountRecordMapper memberAccountRecordMapper;

    @Override
    public MemberAccountRecordMapper getMapper() {
        return memberAccountRecordMapper;
    }

    //是否存在余额变动记录
    @Override
    public int selectByLocalTradeNo(String orderNum) {
        MemberAccountRecordExample memberAccountRecordExample = new MemberAccountRecordExample();
        memberAccountRecordExample.createCriteria().andLocalTradeNoEqualTo(orderNum)
                .andRecordTypeEqualTo("1");

        List<MemberAccountRecord> memberAccountRecords = memberAccountRecordMapper.selectByExample(memberAccountRecordExample);
        if (CollectionUtils.isEmpty(memberAccountRecords)) {
            return 0;
        }
        return 1;
    }

    @Override
    public MemberAccountRecord selectByLocalTradeNoAsc(String orderNum) {
        MemberAccountRecordExample memberAccountRecordExample = new MemberAccountRecordExample();
        memberAccountRecordExample.createCriteria().andLocalTradeNoEqualTo(orderNum)
                .andRecordTypeEqualTo("1");
        memberAccountRecordExample.setOrderByClause(" created_time asc ");

        List<MemberAccountRecord> memberAccountRecords = memberAccountRecordMapper.selectByExample(memberAccountRecordExample);
        if (CollectionUtils.isEmpty(memberAccountRecords)) {
            return null;
        }
        return memberAccountRecords.get(0);
    }
}
